//
//  LPTComCategoryView.m
//  LPTClient
//
//  Created by mac on 2018/12/10.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTComCategoryView.h"
#import "LPTCategoryModel.h"

@interface LPTComCategoryView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView *categoryTableView;
@property (nonatomic,weak)UITableView *medicineTableView;

@property (nonatomic,copy)NSArray *dataSource;
@property (nonatomic,copy)NSArray *origionData;

@property (nonatomic,assign)NSInteger currentCategoryIndex;
@property (nonatomic,weak)UIButton *resetBtn;
@property (nonatomic,weak)UIButton *sureBtn;
@property (nonatomic,strong)NSMutableArray *selectedMedArray;

@end

@implementation LPTComCategoryView

-(instancetype)initWithComArray:(NSArray *)array
{
    self = [super init];
    if (self) {
        
        self.selectedMedArray = [[NSMutableArray alloc] init];
        self.dataSource = [[NSArray alloc] initWithArray:array copyItems:YES];
        self.origionData = array;
        self.frame = CGRectMake(0, -300, kScreenWidth, 300);
        self.currentCategoryIndex = 0;
        
        [self initSubViews];
    }
    return self;
}

-(void)initSubViews
{
    UITableView *cateTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, self.height - 40) style:UITableViewStylePlain];
    cateTableView.delegate = self;
    cateTableView.dataSource = self;
    cateTableView.tableFooterView = [UIView new];
    [cateTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"categoryCell"];
    [self addSubview:cateTableView];
    self.categoryTableView = cateTableView;
    
    UITableView *medicineTableView = [[UITableView alloc] initWithFrame:CGRectMake(100, 0, kScreenWidth - 100, self.height - 40) style:UITableViewStylePlain];
    medicineTableView.delegate = self;
    medicineTableView.dataSource = self;
    medicineTableView.tableFooterView = [UIView new];
    [medicineTableView registerClass:[LPTMedicineCell class] forCellReuseIdentifier:@"LPTMedicineCell"];
    [self addSubview:medicineTableView];
    self.medicineTableView = medicineTableView;
    
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    resetBtn.backgroundColor = kRgbColor(255, 180, 81, 1);
    resetBtn.titleLabel.textColor = [UIColor whiteColor];
    resetBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    [resetBtn addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:resetBtn];
    self.resetBtn = resetBtn;
    [self.resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.bottom.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth/2, 40));
    }];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
    sureBtn.backgroundColor = kThemeOrigionColor;
    sureBtn.titleLabel.textColor = [UIColor whiteColor];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    [sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sureBtn];
    self.sureBtn = sureBtn;
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.bottom.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth/2, 40));
    }];
}

-(void)resetAction:(UIButton *)button
{
    self.dataSource = [[NSArray alloc] initWithArray:self.origionData copyItems:YES];
    self.currentCategoryIndex = 0;
    
    [self setSelectMedicine];
    [self.categoryTableView setContentOffset:CGPointZero];
    [self.categoryTableView reloadData];
    [self.medicineTableView reloadData];
}

-(void)sureAction:(UIButton *)button
{
    [self setSelectMedicine];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(comCategoryViewDidClickSureBtnWithArray:)]) {
        [self.delegate comCategoryViewDidClickSureBtnWithArray:[self.selectedMedArray copy]];
    }
}

-(void)setSelectMedicine
{
    [self.selectedMedArray removeAllObjects];
    for (LPTCategoryModel *cateModel in self.dataSource) {
        
        for (LPTMedicineModel *medicineModel in cateModel.chinese_medicines) {
            
            if (medicineModel.count > 0) {
                [self.selectedMedArray addObject:medicineModel];
            }
        }
    }
}

#pragma mark - UITableViewDataSource & Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.categoryTableView) {
        return self.dataSource.count;
    }else{
        
        if (self.currentCategoryIndex < self.dataSource.count) {
            
            LPTCategoryModel *catemodel = self.dataSource[self.currentCategoryIndex];
            return catemodel.chinese_medicines.count;
        }else{
            return 0;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.categoryTableView) {
        
        LPTCategoryModel *catemodel = self.dataSource[indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"categoryCell" forIndexPath:indexPath];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
        
        if (self.currentCategoryIndex == indexPath.row) {
    
            cell.contentView.backgroundColor = [UIColor whiteColor];
            
        }else{
            
            cell.contentView.backgroundColor = [UIColor colorWithWhite:.5 alpha:1];
        }
        
        cell.textLabel.text = catemodel.name;
        return cell;
        
    }else{
        
        LPTCategoryModel *catemodel = self.dataSource[self.currentCategoryIndex];
        LPTMedicineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTMedicineCell" forIndexPath:indexPath];
        LPTMedicineModel *medicineModel = catemodel.chinese_medicines[indexPath.row];
        [cell refreshWithEntity:medicineModel];
        
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == self.categoryTableView) {
        
        self.currentCategoryIndex = indexPath.row;
        [self.medicineTableView reloadData];
        [self.categoryTableView reloadData];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

//药方cell
@interface LPTMedicineCell ()

@property (nonatomic,weak)UILabel *medicineLabel;
@property (nonatomic,weak)UIButton *minusBtn;
@property (nonatomic,weak)UIButton *addBtn;
@property (nonatomic,weak)UILabel *numberLabel;
@property (nonatomic,strong)LPTMedicineModel *entity;

@end

@implementation LPTMedicineCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self initSubViews];
    }
    
    return self;
}

-(void)initSubViews
{
    UILabel *medicineLabel = [[UILabel alloc] init];
    medicineLabel.font = [UIFont systemFontOfSize:14.0f];
    [self.contentView addSubview:medicineLabel];
    self.medicineLabel = medicineLabel;
    [self.medicineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addBtn setImage:[UIImage imageNamed:@"btn_jia"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addActions:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:addBtn];
    self.addBtn = addBtn;
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.trailing.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(24, 20));
    }];
    
    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.font = [UIFont systemFontOfSize:14.0f];
    [self.contentView addSubview:countLabel];
    self.numberLabel = countLabel;
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.mas_equalTo(self.addBtn.mas_leading).mas_offset(0);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(33);
    }];
    
    UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [minusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [minusBtn setImage:[UIImage imageNamed:@"btn_jian"] forState:UIControlStateNormal];
    [minusBtn addTarget:self action:@selector(mimusAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:minusBtn];
    self.minusBtn = minusBtn;
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.mas_equalTo(self.numberLabel.mas_leading).mas_offset(0);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(24, 20));
    }];
}

-(void)refreshWithEntity:(LPTMedicineModel *)entity
{
    _entity = entity;
    self.medicineLabel.text = entity.name;
    self.numberLabel.text = [NSString stringWithFormat:@"%zd",entity.count];
}

-(void)addActions:(UIButton *)button
{
    self.entity.count ++;
    [self refreshWithEntity:_entity];
}

-(void)mimusAction:(UIButton *)button
{
    if (self.entity.count > 0) {
        
        self.entity.count --;
    }
    [self refreshWithEntity:_entity];
}

@end
